<?php
namespace Admin\Controller;
use Think\Controller;
class TrainController extends Controller
{
    public function index()
    {
        $this->display();
       
    }

    /*培训列表*/
    public function trainlist(){
        $train = M('train');
        $trainlist = $train->order("trainid asc")->field('trainid,trainname,trainintro,trainpic,trainnotice,traintime,trainplace,trainmoney,trainlevel')->select();

        for($index=0;$index<count($trainlist);$index++)
        {
            $trainlist[$index]['number']=$index+1;
        }

        $this->assign('trainlist',$trainlist);
        $this->display();
    }







    /*培训删除*/
    public function deltrain( )
    {

        $trainid=I('trainid');
        $train = M('train');//实例化news对象
        if($train->delete($trainid)) {
            $this->success('删除成功');
        }
        else {
            $this->error('删除失败');
        }
    }

   
//修改
public function edittrain(){
        $train=M('train');
        $trainid=$_GET['trainid'];
        $res = $train->where("trainid=$trainid")->select();      
        $this->trainlist=$res;//name属性对应的值 $this->对应值=$find;
        $this->display();
    }
 public function update(){
           $data['trainname']=$_POST['trainname'];
           $data['trainintro']=$_POST['trainintro'];
           $data['trainnotice']=$_POST['trainnotice'];
           $data['trainmoney']=$_POST['trainmoney'];
           $data['traintime']=$_POST['traintime']; 
            $data['trainlevel']=$_POST['trainlevel']; 
            $data['trainplace']=$_POST['trainplace']; 

           $trainid = $_POST['trainid'];

           $trainModel = M('train'); // 实例化User模型
             $data['createtime'] =  date("Y-m-d H:i:s"); // 设置用户的创建时间
             // print_r($_FILES);
             if ($_FILES['trainpic']['size']>0) {//没有上传轮播图片
                            $oldname = $trainModel->field('trainpic')->where('trainid ='.$trainid)->find();
                            unlink('./Public/Uploads/photo/'.$oldname['trainpic']);

                          $filename = explode('.', $_FILES['trainpic']['name']);
                          $extendsname = end($filename);
                        //  print_r()
                          $savename = rand(0,99).time().'.'.$extendsname;
                          move_uploaded_file($_FILES['trainpic']['tmp_name'], './Public/Uploads/photo/'.$savename);
                          $data['trainpic'] = $savename;
                         
                  }
               //  $addresult=$trainModel->add(); // 写入用户数据到数据库
                  
                     if (false != $trainModel->where('trainid ='.$trainid)->save($data))
                             {
                            $this->success('修改成功！', '../Train/trainlist');
                             }else{ 
                                     $this->error('修改错误'); 
                                   }
                                    } 
 public function Upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
        //$upload->savePath  =     ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $info = $upload->upload();  // 上传文件
       
        if (!$info) {
            $this->error($upload->getError());
        } else {
            //$this->success('上传成功！');
            return $info;
        }
    }




    //添加奖品
    public function addtrain(){
        $this->display();
    }  
        public function newtrain(){

 $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']);
        
        //没有头像
        $trainModel = M('train'); // 实例化User模型
        $createresult=$trainModel->create();   // 根据表单提交的POST数据创建数据对象
          if ($createresult) {
            $trainModel->createtime =  date("Y-m-d H:i:s"); // 设置用户的创建时间
             if ($file['trainpic']['size']==0) {//没有上传轮播图片
                        $trainModel->trainpic= "nopic.jpg"; // 用户不上传头像默认nopic.jpg
                    }else{//有图片
                            $info=$this->Upload();
                            $trainModel->trainpic = $info['trainpic']['savename']; // 保存上传的照片根据需要自行组装
                          }
                 $addresult=$trainModel->add(); // 写入用户数据到数据库
                     if (false !== $addresult)
                             {
                            $this->success('上传成功！', '../Train/trainlist');
                             }else{ 
                                     $this->error('数据错误'); 
                                   }

        }else{
                $this->error($brandsModel->getError());  
              }
 
    }
   
 public function Strainlist(){
        $train = M('train');
        $trainlist = $train->order("trainid asc")->field('trainid,trainname,trainintro,trainpic,trainnotice,traintime,trainplace,trainmoney,trainlevel')->select();

        for($index=0;$index<count($trainlist);$index++)
        {
            $trainlist[$index]['number']=$index+1;
        }

        $this->assign('trainlist',$trainlist);
        $this->display();
    }



    }








